import { app, BrowserWindow, screen, Menu } from "electron";
import path from "node:path";
import { createWebContentsShortcut } from "../shortcut";
import { writeWindowsBound, readWindowsBound } from "./common";

const createIndexWindow = () => {
    let bounds = readWindowsBound("mainWindow");
    console.log(bounds);
    let x = 0;
    let y = 0;
    if (bounds) {
        x = bounds.x;
        y = bounds.y;
    }
    // 全屏显示
    const primaryDisplay = screen.getPrimaryDisplay();
    const { width, height } = primaryDisplay.workAreaSize;

    const mainWindow = new BrowserWindow({
        x,
        y,
        width,
        height,
        webPreferences: {
            preload: path.join(__dirname, "preload.js"),
        },
        icon: path.join(__dirname, "./assets/2.ico"),
        hasShadow: false,
    });
    mainWindow.maximize();

    mainWindow.on("close", () => {
        let { x, y } = mainWindow.getBounds();
        writeWindowsBound(JSON.stringify({ mainWindow: { x, y } }));
        mainWindow.webContents.send("close-window");
    });

    // 取消菜单栏
    Menu.setApplicationMenu(null);
    mainWindow.loadURL("http://localhost:9000/system/roleManage");
    // 打开调试工具
    if (process.env.NODE_ENV == "development") {
        mainWindow.webContents.openDevTools();
    }

    createWebContentsShortcut(mainWindow.webContents);
};

export default createIndexWindow;
